function makemovie(doit, varargin)
% doit: 1 means heed value_name, value combos; 0 means do not heed these values
% valid [value_name, value] combinations:

% open path & name
% close path & name
% addrate {slowdown value : 0 or 1 for none, 2 for 2 frames per, 3 for 3 frames etc}


% typical use:
% top of file DEFINE: 
% GEN_MOV=1;
% top of file do: 
% makemovie(GEN_MOV, 'open','C:\0maciver\ribbon')
% after figure do: 
% makemovie(GEN_MOV, 'addrate','0')
% bottom of file do: 
% makemovie(GEN_MOV, 'close','')

% collection of lines:
% GEN_MOV=1;
% makemovie(GEN_MOV, 'open','C:\0maciver\ribbon')
% makemovie(GEN_MOV, 'addrate','0')
% makemovie(GEN_MOV, 'close','')

if doit
    persistent aviH
    
    for i=1:2:size(varargin,2)
        switch varargin{i}
    
        case 'open'
            pathname = varargin{i+1};
            aviH = avifile(pathname, 'compression','none', ...
                'quality', 100, 'fps', 10);
            
        case 'close'
            fprintf(1, 'created AVI file in %s\n', aviH.Filename);
            aviH = close(aviH);
            clear aviH
            
        case 'addrate'
            
           frame = getframe(gcf);
 %            figureH=gcf;
            slowdown = str2num(varargin{i+1});
            
            if slowdown>0
                for i=1:slowdown
                   
                    aviH = addframe(aviH, frame);
 %                    aviH = addframe(aviH,figureH);
                end
            else

                aviH = addframe(aviH, frame);
  %              aviH = addframe(aviH, figureH);
            end
            
        otherwise
            disp('invalid makemovie parameter type')
            
        end
        
    end
    
end
